<template>
    <div class="content">
        <img src="../../assets/bg/bg.jpeg" alt="" class="bgImg">
        <div class="loginBox">
            <div class="l-content">
                <!-- <img src="../../assets/bg/bg.webp"/> -->
            </div>
            <div class="r-content">
                <div class="head">
                    <h1></h1>
                    <h2>高校图书馆管理系统</h2>
                </div>
                <el-form ref="formRef" :rules="rules" :model="mangerInfo">
                    <el-form-item prop="uid">
                        <el-input placeholder="登录账号" prefix-icon="User" type="text" v-model="mangerInfo.uid" />
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input placeholder="登录密码" prefix-icon="Lock" show-password type="password"
                            v-model="mangerInfo.password" />
                    </el-form-item>
                    <!-- <el-form-item prop="code">
                        <el-input placeholder="输入验证码" prefix-icon="Connection" show-password type="text"
                            v-model="mangerInfo.code" />
                        <div class="codeBox"></div>
                    </el-form-item> -->
                    <el-form-item>
                        <el-checkbox v-model="isRemberPasswd">记住密码</el-checkbox>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="login(formRef)">登录</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref, watch} from 'vue';
import type { FormInstance, FormRules } from 'element-plus'
import {useLoginStore} from '../../stores/loginStore'
const {mangerLogin} =useLoginStore()
const formRef = ref<FormInstance>()
const mangerInfo = ref({
    uid: '',
    password: '',
    code: ''
})
const isRemberPasswd=ref<boolean>(false)


watch(()=>isRemberPasswd.value,(newValue,oldValue)=>{
    if(newValue){
        console.log("密码保存了");
    }else{
        console.log("密码没保存");
        localStorage.removeItem("tempPasswd")
    }
})

onMounted(()=>{
    let temp=JSON.parse(localStorage.getItem('tempPasswd') as string)
    console.log(temp);
    if(temp){
        isRemberPasswd.value=true
        const {uid,password}=temp
        mangerInfo.value.uid=uid
        mangerInfo.value.password=password
    }
})

const checkuid = (rule: any, value: any, callback: any) => {
    if (!value) {
        return callback(new Error('请输入登录账号'))
    } else {
        callback()
    }
}

const checkPassword = (rule: any, value: any, callback: any) => {
    if (!value) {
        return callback(new Error('请输入登录密码'))
    } else {
        callback()
    }
}

const checkCode = (rule: any, value: any, callback: any) => {
    if (!value) {
        return callback(new Error('请输入验证码'))
    } else {
        callback()
    }
}

const rules = reactive<FormRules>({
    uid: [{ validator: checkuid, trigger: 'blur' }],
    password: [{ validator: checkPassword, trigger: 'blur' }],
    code: [{ validator: checkCode, trigger: 'blur' }],
})


const login = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            const {uid,password}=mangerInfo.value
            if(isRemberPasswd.value)localStorage.setItem('tempPasswd',JSON.stringify({uid,password}))
            mangerLogin({uid,password})
        } else {
            console.log('error submit!')
            return false
        }
    })
}


</script>

<style scoped lang="scss">
.content {
    position: relative;
    height: 100vh;
    width: 100%;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: darkblue;

    .bgImg {
        position: absolute;
        width: 100%;
        height: auto;
        left: 50%;
        top: 50%;
        transform: translate3d(-50%, -50%, 0);
        opacity: 0.8;
    }

    .loginBox {
        position: relative;
        z-index: 1;
        width: 1000px;
        height: 600px;
        background-color: #fff;
        border-radius: 18px;
        display: flex;
        overflow: hidden;
        align-items: center;
        
        .l-content {
            width: 60%;
            height: 100%;
            background: #0da4e5;
            overflow: hidden;
            position: relative;
            background:url('../../assets/bg/bg.webp');
            background-size: cover;
            // img {
            //     height: 100%;
            //     position: absolute;
            //     right: -50px;
            // }
        }

        .r-content {
            height: 100%;
            width: 50%;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: flex-start;

            .head {
                margin-top: 50px;
                display: flex;
                align-items: center;
                flex-direction: column;

                h1 {
                    height: 120px;
                    width: 120px;
                    background: url('../../assets/bg/logo.png');
                    background-size: cover;
                }

                h2 {
                    margin-top: 20px;
                    font-size: 22px;
                    text-align: center;
                }
            }

            .el-form {
                margin-top: 40px;
                width: 300px;

                .el-form-item {
                    .el-input {
                        height: 40px;
                    }

                    &:nth-child(3) {
                        width: 300px;
                        display: flex;
                        justify-content: space-between;

                        .el-input {
                            width: 190px;
                        }

                        .codeBox {
                            margin-left: 10px;
                            width: 100px;
                            height: 40px;
                            border: 1px solid gray;
                            box-sizing: border-box;
                        }
                    }

                }

                .el-button {
                    width: 300px;
                    height: 40px;
                }
            }
        }
    }
}</style>